import {Component, Input, OnInit} from '@angular/core';
import {SFSchema} from '@delon/form';
import {NzMessageService} from 'ng-zorro-antd/message';
import {SysConfigService} from '../service/sys-config.service';
import {ActivatedRoute} from '@angular/router';
import {SysAttachmentUploadSchema} from '../../sys-attachment/sys-attachment-upload/sys-attachment-upload-schema';

@Component({
    selector: 'sys-config-page',
    templateUrl: './sys-config-page.component.html',
    styleUrls: ['./sys-config-page.component.less']
})
export class SysConfigPageComponent implements OnInit {
    get code(): string {
        return this._code;
    }

    @Input()
    set code(value: string) {
        this._code = value;

        this.updateByCode();
    }

    schema: SFSchema;

    formData = {};

    private _code: string;

    init = false;

    constructor(private sysConfigService: SysConfigService,
                private nzMessageService: NzMessageService,
                private activatedRoute: ActivatedRoute) {
    }

    ngOnInit(): void {
        if (this.code) {
            return;
        }
        let code = this.activatedRoute.snapshot.queryParams['code'];
        if (!code) {
            code = this.activatedRoute.snapshot.params['code'];
        }
        if (!code && this.activatedRoute.snapshot.data.param) {
            code = this.activatedRoute.snapshot.data.param['code'];
        }
        if (code) {
            this.code = code;
        } else {
            this.nzMessageService.error('需要参数code!');
        }
    }

    updateByCode() {
        this.sysConfigService.getSchema(this._code).subscribe(returnForm => {
            if (returnForm.success) {
                const schemaMap = JSON.parse(returnForm.message);
                let schema: SFSchema = null;
                for (const key in schemaMap) {
                    schema = schemaMap[key] as SFSchema;
                    break;
                }

                for (const propertiesKey in schema.properties) {
                    const s = schema.properties[propertiesKey];
                    // @ts-ignore
                    if (s.ui && s.ui.widget === 'sys-attachment-upload') {
                        const uploadSchema = schema.properties[propertiesKey].ui as SysAttachmentUploadSchema;
                        uploadSchema.baseAttachmentParam = {
                            page: this.code
                        };
                        uploadSchema.baseAttachmentService = this.sysConfigService;
                    }
                }

                // 设置schema中对应的file模式
                this.schema = schema;
                this.sysConfigService.getValue(this._code).subscribe(returnForm2 => {
                    if (returnForm2.success) {
                        if (returnForm2.message) {
                            this.formData = returnForm2.message;
                        }
                        this.init = true;
                    }
                });
            }
        });
    }

    submit(value: any) {
        value = Object.assign({}, value);
        this.sysConfigService.updateValue(this._code, value).subscribe(returnForm => {
            if (returnForm.success) {
                this.formData = returnForm.message;
                this.nzMessageService.info('保存成功！');
            } else {
                this.nzMessageService.info('保存失败！');
            }
        });
    }
}
